package thread;

class TestLock{
	
	Object key=new Object();
	
	public synchronized void test1() {
		for (int i = 0; i < 5; i++) {
			System.out.println("方法1....");
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public void test2() {
		
		synchronized (this) {
			for (int i = 0; i < 5; i++) {
				System.out.println("方法2....");
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		} 
	}
	
}

public class Test6 {

	public static void main(String[] args) {
		TestLock lock=new TestLock();
		
		 
		Thread t1=new Thread(new Runnable() {
			public void run() {
				lock.test1();
			}
		});
		
		Thread t2=new Thread(new Runnable() {
			public void run() {
				lock.test2();
			}
		});
		
		t1.start();
		t2.start();

	}

}
